package com.shop.front.controller;

import com.shop.common.entity.CommonResult;
import com.shop.front.feign.LoginFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ghy
 * @version 1.0
 * @date 2020-11-02
 */
@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    RestTemplate restTemplate;

    //@Autowired
    //LoginFeign loginFeign;

    /**
     * 根据login_token去redis获取用户信息
     * @param token
     * @return
     */
    @GetMapping("/name")
    public CommonResult name(@CookieValue(name = "login_token", required = false) String token){
        //对接登录模块获取用户名的接口
        //获取这个 http://localhost:8001/login/name 的返回值
        //return loginFeign.name(token);
        //String url = "http://localhost:8001/login/name";
        String url = "http://shop-user-service/login/name";
        //设置请求头，cookie头
        HttpHeaders headers = new HttpHeaders();

        List<String> list = new ArrayList<>();
        list.add("login_token=" + token);

        headers.put(HttpHeaders.COOKIE, list);

        HttpEntity request = new HttpEntity(headers);

        return restTemplate.postForObject(url, request, CommonResult.class);
    }

}
